package com.me.Inheritor;

import java.util.ArrayList;
import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.math.Vector2;

public class LevelGenerator 
{
	Random rand;
	ArrayList<SectionPattern> patterns;
	SectionPattern curPattern;
	
	float fTimeToNextGeneration;
	float fCurTime;
	
	public LevelGenerator()
	{
		rand = new Random();
		patterns = new ArrayList<SectionPattern>();
		curPattern = null;
		
		fTimeToNextGeneration = 1;
		fCurTime = 0;
		
		LoadPattern( "patterns/blue.pat" );
		LoadPattern( "patterns/red.pat" );
		LoadPattern( "patterns/yellow.pat" );
	}
	
	void LoadPattern( String FileName )
	{
		FileHandle handle = Gdx.files.internal( FileName );
		String data = handle.readString();
		SectionPattern pattern = new SectionPattern();

		float time = 0;
		int y = 0;
		int width = 0;
		int height = 0;
		int type = 0;
		
		String	str = "";
		int index = 0;
		
		// Convert the string data into actual data
		for( int i = 0;i < data.length();++i )
		{
			if( data.charAt(i) != ' ' && data.charAt(i) != '\n' && data.charAt(i) != '\0' )
				str += data.charAt(i);
			else if( data.charAt(i) == ' ' )
			{
				if( index == 0 )
					time = Float.parseFloat(str);
				if( index == 1 )
					y = Integer.parseInt(str);
				if( index == 2 )
					width = Integer.parseInt(str);
				if( index == 3 )
					height = Integer.parseInt(str);

				++index;
				str = "";
			}
			else if( data.charAt(i) == '\n' || data.charAt(i) == '\0' )
			{
				str = str.trim();
				
				if( index == 4 )
					type = Integer.parseInt( str );
				index = 0;
				str = "";
				
				PatternObj obj = new PatternObj();
				
				obj.fSpawnTime	= time;
				obj.position.y	= y;
				obj.position.x	= 0;
				obj.width		= width;
				obj.height		= height;
				
				switch( type )
				{
				case 0:
					obj.objType = ObjectManager.ObjectType.OT_Platform;
				case 1:
					obj.objType = ObjectManager.ObjectType.OT_Platform;
				case 2:
					obj.objType = ObjectManager.ObjectType.OT_Platform;
				}
				
				pattern.AddPatternObj( obj );
			}
		}
		
		patterns.add( pattern );
	}
	
	public void Update( float dt )
	{
		if( curPattern != null )
		{
			curPattern.Update( dt );
			
			// Go through all objects that should be spawning now
			Platform platform = curPattern.getNextObj();
			while( platform != null )
				platform = curPattern.getNextObj();
			
			if( curPattern.Finished() )
			{
				curPattern = null;
				fTimeToNextGeneration = rand.nextFloat() * 3 + 2;
			}
		}
		else // A pattern is not currently being generated
		{
			fCurTime += dt;
			
			if( fCurTime >= fTimeToNextGeneration )
			{
				// Pick a random pattern
				curPattern = new SectionPattern( patterns.get( rand.nextInt( patterns.size() )));
				fCurTime = 0;
			}
		}
	}
	
	
	@SuppressWarnings("unused")
	private void DebugPrint( String str )
	{
		System.out.println( str );
	}
	
	class SectionPattern
	{
		ArrayList< PatternObj > objects;
		float	fTotalTime;
		
		public SectionPattern()
		{
			objects = new ArrayList< PatternObj >();
			fTotalTime = 0;
		}
		public SectionPattern( SectionPattern copy )
		{
			objects = new ArrayList< PatternObj >();
			
			for( int i = 0;i < copy.objects.size();++i )
			{
				objects.add( new PatternObj( copy.objects.get(i) ) );
			}
			
			fTotalTime = 0;
		}
		
		public void AddPatternObj( PatternObj obj )
		{
			objects.add( obj );
		}
		
		public void Update( float dt )
		{
			fTotalTime += dt;
		}
		
		public Platform getNextObj()
		{
			for( int i = 0;i < objects.size();++i )
			{
				if( objects.get(i).fSpawnTime <= fTotalTime )
				{
					Platform obj = (Platform)GameData.getObjectManager().CreateObject( objects.get(i).objType );
					
					obj.setWidth( objects.get(i).width );
					obj.setHeight( objects.get(i).height );
					obj.setX( Gdx.graphics.getWidth()*2 );
					obj.setY( objects.get(i).position.y );
					
					objects.remove(i);
					return obj;
				}
			}
			
			return null;
		}
		public boolean Finished()
		{
			if( objects.size() > 0 )
				return false;
			
			return true;
		}

	}

	public class PatternObj
	{
		public ObjectManager.ObjectType objType;
		public Vector2		position;
		public float		width;
		public float		height;
		public float		fSpawnTime;
		
		public PatternObj()
		{
			fSpawnTime = 0;
			position = new Vector2(0,0);
			width = 0;
			height = 0;
		}
		public PatternObj( PatternObj copy )
		{
			fSpawnTime = copy.fSpawnTime;
			objType = copy.objType;
			position = new Vector2(0,0);
			
			width = copy.width;
			height = copy.height;
			position.x = copy.position.x;
			position.y = copy.position.y;
		}
	}
}
